package view.overlay;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import view.drawing.ImageLibrary;

public class ScrollingTextOverlay extends ActiveOverlay {
	
	private String[] mText;
	private long mScrollRate;
	private boolean mScrolling;
	
	private int mScrollPosition;
	
	private int mLineSpacing = 20;
	
	private long mTimeToUpdate;
	private long mLastUpdate;
	
	private OnScrollFinishListener mScrollFinishListener;
	
	private Font mTextFont;
	
	private int mLastLineYOffset;
	
	private Image mBackgroundImage;
	
	public ScrollingTextOverlay() {
		mText = new String[0];
		init();
	}
	
	public ScrollingTextOverlay(String textToScroll) {
		if (textToScroll == null) textToScroll = "";
		mText = textToScroll.split("\n");
		
		init();
		
	}
	
	public ScrollingTextOverlay(File textFile) {
		if (! textFile.exists()) {
			mText = new String[0];
		}
		else {
			
			loadTextFromFile(textFile);
		}
		
		init();
	}
	
	private void init() {
		mScrollRate = 20;
		
		mTextFont = new Font("Courier", Font.PLAIN, 18);
		
		mLastLineYOffset = 9001;
		
	}
	
	public void loadTextFromFile(File file) {
		try {
			String line = "";
			String all = "";
			BufferedReader reader = new BufferedReader(new FileReader(file));
			
			while ((line = reader.readLine()) != null) {
				// append to all
				all += line + "\n";
			}
			
			// now set the text
			mText = all.split("\n");
		}
		catch (IOException ex) {
			mText = new String[0];
		}
	}
	
	public void reset() {
		mScrollPosition = 0;
	}

	@Override
	protected void paint(Graphics g) {
		
		// Set the anti-aliasing on
		Graphics2D g2d = (Graphics2D) g;
		
		Object oldHinting = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		
		g2d.clearRect(0, 0, this.getWidth(), this.getHeight());
		// Draw the background image if there is one
		if (mBackgroundImage != null) {
			g2d.drawImage(mBackgroundImage, 0, 0, this.getWidth(), this.getHeight(), null);
		}
		
		g2d.setFont(mTextFont);
		g2d.setColor(Color.WHITE);
		
		// paint the overlay
		if (mScrolling) {
			if (mTimeToUpdate <= 0) {
				mTimeToUpdate = mScrollRate;
				mScrollPosition += 2;
				
				if (mLastLineYOffset <= 0) {
					scrollFinish();
				}
			}
			else {
				mTimeToUpdate -= (System.currentTimeMillis() - mLastUpdate);
				mLastUpdate = System.currentTimeMillis();
			}
		}
		
		// Draw the text
		for (int i = 0; i < mText.length; i++) {
			// Measure the string
			Rectangle2D bounds = g.getFontMetrics().getStringBounds(mText[i], g);
			int x = (int)(this.getWidth() / 2 - bounds.getWidth() / 2);
			int y = (int)(this.getHeight() - mScrollPosition + i * mLineSpacing * 2);
			
			g2d.drawString(mText[i], x, y);
			
			if (i == mText.length - 1) {
				// Last line
				mLastLineYOffset = y;
			}
		}
		
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldHinting);
	}
	
	private void scrollFinish() {
		// notify the listener we are finished
		if (mScrollFinishListener != null) {
			mScrollFinishListener.onFinish();
		}
	}

	@Override
	public void resize(int width, int height) {
		super.resize(width, height);
	}
	
	public void setBackgroundImage(Image bgImage) {
		this.mBackgroundImage = bgImage;
	}
	
	public void setBackgroundImage(File imageFile) {
		this.mBackgroundImage = ImageLibrary.getInstance().getImage(imageFile);
	}
	
	@Override
	public Dimension getPreferredSize() {
		return (new Dimension(0, 0));
	}
	
	public void start() {
		mScrolling = true;
		
		// this wasn't working properly. so i changed it to over 9000 and it did.
		mLastLineYOffset = 9001;
	}
	
	public void stop() {
		mScrolling = false;
	}
	
	public void setScrollRate(long rate) {
		mScrollRate = rate;
	}
	
	public void setOnScrollFinishListener(OnScrollFinishListener listener) {
		this.mScrollFinishListener = listener;
	}
	
	public interface OnScrollFinishListener {
		public void onFinish();
	}

}
